package com.tax.common.util;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

public class TemplateUtil {
	
	private static final String RESOURCE_NAME = "template";
	
	/**
	 * 执行模板替换
	 * @param templateContent 模板内容
	 * @param params 模板参数
	 * @return
	 * @throws IOException
	 */
	public static String execute(String templateContent, Map<String, Object> params) throws IOException {
		String result;
		Writer out = null;
		
		try {
			out = new StringWriter();
			MustacheFactory mf = new DefaultMustacheFactory();
			Mustache mustache = mf.compile(new StringReader(templateContent), RESOURCE_NAME);
			mustache.execute(out, params);
			result = out.toString();
		} finally {
			// 使用StringWriter不必做flush和close
//			out.flush();
//			out.close();
		}
		
		return result;
	}
	
//	public static void mai1n(String[] args) throws IOException {
//		String title = "【巡检单异常预警】巡检单{{entity.id}}{{entity.description}}巡检过期时间{{entity.expDate}}即将到，请及时处理。";
//		Map<String, String> params = new HashMap<>();
//		params.put("entity.id", "123");
//		params.put("entity.description", "测试单号");
//		params.put("entity.expDate", DateTime.now().toString(Constant.DATETIME_FORMATER_NORM));
////		params.put("entity.expDate1", DateTime.now().toString(Constant.DATETIME_FORMATER_NORM));
//		
//		String cnt = execute(title, params);
//		System.out.println(cnt);
//	}

}
